/**
 * creat by liuqiang
 * showLoading {type: Boolean} 是否需要加载提示
 * url         {type: String} 接口地址
 * data        {type: Object} 请求参数
 * method      {type: String} 请求方式 get,post 默认get
 */
function _http(showLoading, url, data, method,) {
  const host = 'http://test.wangyangyang.vip/api/'; // 接口域名
  if(showLoading){
    wx.showLoading({
      title: '加载中',
    })
  }
  return new Promise((resolve, reject) => {
    wx.request({
      url: host+url,
      data: data,
      method: method,
      header: {
        'content-type': 'application/json' // 默认值
      },
      responseType: arraybuffer,
      success (res) {
        wx.hideLoading();
        resolve(res.data)
      },
      fail: function(error) {
        wx.hideLoading();
        reject(false)
      },
      complete: function() {
        wx.hideLoading();
      }
    })
  })
}

function _get(obj) {
  return _http(obj.showLoading, obj.url, obj.data, 'GET')
}

function _post(obj) {
  return _http(obj.showLoading, obj.url, obj.data, 'POST')
}

export default {
  _http,
  _get,
  _post
}